package com.imooc.framework.excel;

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

/**
 * @author xxms
 */
public class ExcelWriteTest
{

    public static void main(String[] args) throws Exception {
        POIExcel03();
        POIExcel07();
    }

    public static void POIExcel03() throws Exception {
        String PATH = "D:/";
        // 1、创建一个工作簿 03
        Workbook workbook = new HSSFWorkbook();
        // 2、创建一个工作表
        Sheet sheet = workbook.createSheet("xxms观众统计表");
        // 3、创建一个行
        Row row1 = sheet.createRow(0);
        // 4、创建一个单元格 (1,1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日新增观众");
        // (1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);

        // 第二行
        Row row2 = sheet.createRow(1);
        //(2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        // (2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        // 生成一张表(IO流) 03版本就是使用xls结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms观众统计表03.xls");

        workbook.write(fileOutputStream);

        // 关闭流
        fileOutputStream.close();

        System.out.println("xxms观众统计表03 生成完毕");
    }

    public static void POIExcel07() throws Exception {
        String PATH = "D:/";
        // 1、创建一个工作簿 07
        Workbook workbook = new XSSFWorkbook();
        // 2、创建一个工作表
        Sheet sheet = workbook.createSheet("xxms观众统计表");
        // 3、创建一个行
        Row row1 = sheet.createRow(0);
        // 4、创建一个单元格 (1,1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日新增观众");
        // (1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);

        // 第二行
        Row row2 = sheet.createRow(1);
        //(2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        // (2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        // 生成一张表(IO流) 07版本就是使用xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms观众统计表07.xlsx");

        workbook.write(fileOutputStream);

        // 关闭流
        fileOutputStream.close();

        System.out.println("xxms观众统计表07 生成完毕");
    }
}

